package com.xunlai.infra.permission.business.controller;

import cn.hutool.core.lang.UUID;
import com.xunlai.infra.permission.business.service.PosterService;
import com.xunlai.infra.permission.business.vo.poster.*;
import com.xunlai.infra.permission.common.constant.ExtendClaimsConstants;
import com.xunlai.infra.permission.common.exception.ExceptionCodeEnum;
import com.xunlai.infra.permission.common.exception.PermissionBusinessException;
import com.xunlai.infra.permission.common.security.TenantUser;
import com.xunlai.infra.permission.common.vo.BasePageRespVo;
import com.xunlai.infra.permission.common.vo.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liang
 * @date 8/7/23 9:56 PM
 */
@RestController
@RequestMapping("travel/manage/poster")
public class PosterManageController {

    private PosterService posterService;

    @PostMapping("create")
    public BaseResponse<Long> createPosterTemplate(@RequestBody PosterCreateVo posterCreateVo) {
        try {
            return BaseResponse.success(posterService.createPoster(posterCreateVo));
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), e.getMsg());
        }
    }

    @RequestMapping(value = "update/{id}", method = {RequestMethod.POST, RequestMethod.PUT})
    public BaseResponse<Void> updatePosterTemplate(@PathVariable Long id, @RequestBody PosterCreateVo posterCreateVo) {
        posterService.updatePoster(id,posterCreateVo);
        return BaseResponse.success();
    }

    @GetMapping("list")
    public BaseResponse<BasePageRespVo<PosterListVo>> listPosterTemplate(@RequestParam(required = false) Long posterType,
                                                                                 @RequestParam(required = false) String posterName,
                                                                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof TenantUser tenantUser){
            String id = tenantUser.getId();
            return BaseResponse.success(posterService.listPoster(Long.valueOf(id), posterType,posterName, pageNum, pageSize));
        }else {
            return BaseResponse.error(ExceptionCodeEnum.NOT_LOGIN);
        }
    }

    @GetMapping("detail/{id}")
    public BaseResponse<PosterDetailVo> getDetail(@PathVariable Long id){
        return BaseResponse.success(posterService.getPoster(id));
    }

    @DeleteMapping("delete/{id}")
    public BaseResponse<Void> delete(@PathVariable Long id){
        posterService.deletePoster(id);
        return BaseResponse.success();
    }
    @GetMapping("detailByNo")
    public BaseResponse<PosterDetailVo> getDetailByPosterNo(@RequestParam("posterNo") String posterNo){
        return BaseResponse.success(posterService.getPosterByPosterNo(posterNo));
    }
    @GetMapping("uuid")
    public BaseResponse<String> uuid(){
        return BaseResponse.success(UUID.randomUUID().toString(true));
    }

    @Autowired
    public void setPosterService(PosterService posterService) {
        this.posterService = posterService;
    }
}
